//
//  SGWindowToolbarDelegate.m
//  iPing
//
//  Created by Thomas on 8/14/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SGWindowToolbarDelegate.h"


@implementation SGWindowController (SGWindowToolbarDelegate)

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
	 itemForItemIdentifier:(NSString *)itemIdentifier
 willBeInsertedIntoToolbar:(BOOL)flag
{
	NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
	NSLog(@"item created: %@",itemIdentifier);
    if ( [itemIdentifier isEqualToString:@"Grabber"] ) {
		[item setLabel:@"Grabber"];
		[item setPaletteLabel:[item label]];
		[item setImage:[NSImage imageNamed:@"Download"]];
		[item setTarget:self];
		[item setAction:@selector(selectView:)];
		[item setTag:0];
    } else if ( [itemIdentifier isEqualToString:@"View"] ) {
		[item setLabel:@"View"];
		[item setPaletteLabel:[item label]];
		[item setImage:[NSImage imageNamed:@"Magnifier"]];
		//[item setTarget:myCurrentViewController];
		[item setTarget:self];
		[item setAction:@selector(view:)];
		[item setTag:0];
    } else if ( [itemIdentifier isEqualToString:@"Library"] ) {
		[item setLabel:@"Library"];
		[item setPaletteLabel:[item label]];
		[item setImage:[NSImage imageNamed:@"scroll"]];
		[item setTarget:self];
		[item setAction:@selector(selectView:)];
		[item setTag:1];
    } else if ( [itemIdentifier isEqualToString:@"TestButton"] ) {
		[item setLabel:@"Test Button"];
		[item setPaletteLabel:[item label]];
		[item setImage:[NSImage imageNamed:@"Star"]];
		[item setTarget:myCurrentViewController];
		[item setAction:@selector(doAddRandom:)];
		[item setTag:1];
    } else if ( [itemIdentifier isEqualToString:@"NextChapter"] ) {
		NSLog(@"NextChapter");
		[item setLabel:@"Next"];
		[item setPaletteLabel:[item label]];
		[item setImage:[NSImage imageNamed:@"Right"]];
		[item setTarget:myCurrentViewController];
		[item setAction:@selector(changeChapter:)];
		[item setTag:1];
    } else if ( [itemIdentifier isEqualToString:@"PreviousChapter"] ) {
		NSLog(@"PreviousChapter");
		[item setLabel:@"Next"];
		[item setPaletteLabel:[item label]];
		[item setImage:[NSImage imageNamed:@"Left"]];
		[item setTarget:myCurrentViewController];
		[item setAction:@selector(changeChapter:)];
		[item setTag:0];
    }else if ([itemIdentifier isEqualToString:@"SearchField"]) {
		[item setLabel:@"Search"];
		[item setPaletteLabel:@"Search"];
		[item setToolTip:@"Search Your Document"];
		[item setView:searchFieldOutlet];
		[item setMinSize:NSMakeSize(50,NSHeight([searchFieldOutlet frame]))];
		[item setMaxSize:NSMakeSize(200,NSHeight([searchFieldOutlet frame]))];
		/*NSMenu *submenu=[[[NSMenu alloc] init] autorelease];
		NSMenuItem *submenuItem=[[[NSMenuItem alloc] initWithTitle: @"Search Panel"
															action:@selector(setSearchCategoryFrom:)
													 keyEquivalent: @""] autorelease];
		NSMenuItem *menuFormRep=[[[NSMenuItem alloc] init] autorelease];
		[submenuItem setTarget:myCurrentViewController];

		[submenuItem setTag:0];
		[submenu addItem: submenuItem];
		[menuFormRep setSubmenu:submenu];
		[menuFormRep setTitle:[item label]];
		[item setMenuFormRepresentation:menuFormRep];*/
	} else if([itemIdentifier isEqualToString:@"Remove"])
	{
		[item setLabel:@"Delete"];
		[item setPaletteLabel:@"Remove Item"];
		[item setImage:[NSImage imageNamed:@"Minus"]];
		[item setTarget:myCurrentViewController];
		[item setAction:@selector(removeSelected:)];
		[item setTag:0];
	} else if ([itemIdentifier isEqualToString:@"PopUpChapter"]) {
		[item setLabel:@"Chapter Selection"];
		[item setPaletteLabel:@"Chapter Selection"];
		[item setToolTip:@"Select Chapter"];
		[item setView:popUpButton];
		[item setMinSize:NSMakeSize(50,NSHeight([popUpButton frame]))];
		[item setMaxSize:NSMakeSize(200,NSHeight([popUpButton frame]))];
		
		/*NSMenu *submenu=[[[NSMenu alloc] init] autorelease];
		NSMenuItem *submenuItem=[[[NSMenuItem alloc] initWithTitle: @"Search Panel"
															action:@selector(setChapterInWebView:)
													 keyEquivalent: @""] autorelease];
		 NSMenuItem *menuFormRep=[[[NSMenuItem alloc] init] autorelease];
		 [submenuItem setTarget:myCurrentViewController];
		 [submenuItem setTag:0];
		 [submenu addItem: submenuItem];
		 [menuFormRep setSubmenu:submenu];
		 [menuFormRep setTitle:@"hello"];*/
		 //[popUpButton setMenuFormRepresentation:submenu];
		[popUpButton removeAllItems];
		NSArray *storyArray = [NSArray arrayWithContentsOfFile:[TCSGGeneralMethods plistPathForHTMLCode:[myCurrentViewController getHTMLCode]]];
		int i;
		[popUpButton addItemWithTitle:[NSString stringWithFormat:@"Complete Story",nil]];
		for (i=0;i<[storyArray count];i++)
		{
			
			[popUpButton addItemWithTitle:[NSString stringWithFormat:@"Chapter %i",(i+1),nil]];
		}
		[popUpButton setTarget:myCurrentViewController];
		[popUpButton setAction:@selector(setChapterInWebView:)];
		//[popUpButton addItemsWithTitles:[NSArray arrayWithObjects:@"Chapter 1",@"Chapter 2",nil]];
	} 
		
	
	
    return [item autorelease];
}

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
	return [NSArray arrayWithObjects:NSToolbarSeparatorItemIdentifier,
			NSToolbarSpaceItemIdentifier,
			NSToolbarFlexibleSpaceItemIdentifier,
			NSToolbarCustomizeToolbarItemIdentifier,
			@"PreviousChapter",
			@"PreviousChapter",
			@"NextChapter",
			@"Grabber",@"View",@"Library",@"TestButton",@"SearchField",@"PopUpChapter", nil];
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
	NSArray *hello;
	if([[toolbar identifier] isEqualToString:@"LibraryToolbar"])
	{
		hello = [NSArray arrayWithObjects:@"Grabber",@"View",@"Remove",NSToolbarFlexibleSpaceItemIdentifier,@"SearchField",
				 NSToolbarCustomizeToolbarItemIdentifier, nil];
	}
	else if([[toolbar identifier] isEqualToString:@"GrabberToolbar"])
	{
		hello = [NSArray arrayWithObjects:@"Library",NSToolbarFlexibleSpaceItemIdentifier,@"TestButton",
				 NSToolbarCustomizeToolbarItemIdentifier, nil];
	} else if([[toolbar identifier] isEqualToString:@"BrowserToolbar"])
	{
		hello = [NSArray arrayWithObjects:@"Grabber",@"Library",NSToolbarFlexibleSpaceItemIdentifier,@"PreviousChapter",@"PopUpChapter",@"NextChapter",NSToolbarFlexibleSpaceItemIdentifier,NSToolbarCustomizeToolbarItemIdentifier,nil];
	}
	NSLog(@"hello: %@", hello);
	return hello;
}
- (IBAction)view:(id)pId
{
	NSLog(@"view");
	[self changeViewController:2];
}
@end
